<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/10/29
 * Time: 15:39
 */
namespace zhtfrontend\modules\sitepadapi\controllers;


use zhtfrontend\modules\sitepadapi\components\rest\ActiveController;
use yii\web\Response;

class TimeController extends ActiveController
{
    public $modelClass = 'zhtfrontend\models\datas\Sales';

    public $serializer = [
        'class' => 'zhtfrontend\modules\sitepadapi\components\rest\Serializer',
        'collectionEnvelope' => 'items',
    ];

    public function behaviors()
    {
        $behaviors = parent::behaviors();
        $behaviors['contentNegotiator']['formats']['text/html'] = Response::FORMAT_JSON;
        return $behaviors;
    }

    public function actions()
    {
        $actions = parent::actions();
        unset($actions['index'], $actions['create'], $actions['update'], $actions['delete'], $actions['view']);
        return $actions;
    }

    public function actionIndex()
    {
        $time_data = $time = [];
        $modelClass = $this->modelClass;
        $query = $modelClass::find()->where(['type' => 0])->asArray()->all();
        foreach ($query as $k => $v) {
            //$time[] = $v['class_day'] . $v['start_time'] . '-' . $v['end_time'];
            $time[]=$this->getTime($v['class_day'],$v['start_time'],$v['end_time']);
        }
        foreach (array_unique($time) as $kk => $vv) {
            $time_data[] = $vv;
        }
        return $time_data;
    }

    protected function getTime($classDay, $startTime, $endTime)
    {
        switch ($classDay) {
            case 1:
                $day = '周一';
                break;
            case 2:
                $day = '周二';
                break;
            case 3:
                $day = '周三';
                break;
            case 4:
                $day = '周四';
                break;
            case 5:
                $day = '周五';
                break;
            case 6:
                $day = '周六';
                break;
            case 7:
                $day = '周日';
                break;
        }
        $startTime = strlen($startTime) == 3 ? substr($startTime, 0, 1) . ':' . substr($startTime, 1) : substr($startTime, 0, 2) . ':' . substr($startTime, 2);
        $endTime = strlen($endTime) == 3 ? substr($endTime, 0, 1) . ':' . substr($endTime, 1) : substr($endTime, 0, 2) . ':' . substr($endTime, 2);
        return $day . '/' . $startTime . '-' . $endTime;
    }


    
}